package com.game.spacetrader.TradeGoods;
import java.io.Serializable;
import java.util.*;

import com.game.spacetrader.Planet;

/**
 * class represents a trade good in the game
 */
public class TradeGoods implements Serializable {
        /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
		int mtlp,mtlu,ttp,ipl,var,basePrice,quantityHave,quantity,price;
        Planet planet;
        Random rand;
        String name;
        /**
         * constructor
         * @param basePrice
         * @param mtlp
         * @param mtlu
         * @param ttp
         * @param ipl
         * @param var
         */
        public TradeGoods(String name,int basePrice,int mtlp,int mtlu, int ttp, int ipl, int var,Planet p) {
                this.mtlp = mtlp;
                this.mtlu = mtlu;
                this.ttp = ttp;
                this.ipl = ipl;
                this.var = var;
                this.name = name;
                this.planet = p;
                rand = new Random();
                this.quantity = 0;
        }
        
        public TradeGoods(String name,int basePrice,int mtlp,int mtlu, int ttp, int ipl, int var,int quantityHave) {
                this.mtlp = mtlp;
                this.mtlu = mtlu;
                this.ttp = ttp;
                this.ipl = ipl;
                this.var = var;
                this.name = name;
                this.quantityHave = 0;
                rand = new Random();
                this.quantity = 0;
        }
        
        /**
         * (the base price) + (the IPL * (Planet Tech Level - MTLP))
         * calculates the price of the item
         * 
         * @param  p
         * @return int price
         */
        public int price() {
        	if(name.equals("gas")){
        		this.price = 10;
        		return price;
        	}
                int planetTechLevel = planet.getTechLevel();
                int eventNum = planet.getEvent().getEventNum();
                this.price = Math.abs(basePrice+(ipl*(planetTechLevel-mtlp)))+1;
                
                switch (eventNum){
                case 1: //drought
                	if(name.equals("Water")){
                		this.price *= 4;
                		return price;
                	}
                	return price;
                case 2: //prospiraty
                	this.price/=2;
                	return price;
                case 3: //depression
                	this.price*=2;
                	return price;
                case 4: //famine
                	if(name.equals("Food")){
                		this.price *=4;
                		return price;
                	}
                	return price;
                default:
                	return price;
                }
                
        }
        
        /**
         * @return Price of item
         */
        public int getPrice() {
        	return price;
        }
        /**
         * Calculates the quantity
         * 
         * @param Planet p (planet for the quantity)
         */
        public void calcQuantity(Planet p) {
        	this.quantityHave = rand.nextInt(var*2+1)+1+planet.getResources();
        }
        
        /**
         * Returns Minimum Tech Level to Produce this resource
         * 
         * @return mtlp
         */
        public int getMtlp() {
			return mtlp;
		}
        
        /**
         * Minimum Tech Level to Use this resource (You can't sell on planets below this level)
         * 
         * @return mtlu
         */
		public int getMtlu() {
			return mtlu;
		}
		
		/**
         * @return quantity that it currently has
         */
		public int getQuantityHave() {
                return quantityHave;
        }
		
		/**
         * @param quantity to set
         */
        public void setQuantityHave(int quantityHave) {
                this.quantityHave = quantityHave;
        }
        
        /**
         * @return quantity
         */
        public int getQuantity() {
                return quantity;
        }
        
        /**
         * @param quantity set the quantity
         */
        public void setQuantity(int quantity) {
                this.quantity = quantity;
        }
        
        /**
         * @return TradeGoods name
         */
        public String getName() {
        	return name;
        }
        /**
         * Resets the quantity to the planets original
         * 
         * @param Planet currentPlanet
         */
        
        public void resetQuantityHave(Planet planet) {
        	this.planet = planet;
        	this.quantityHave = (planet.getTechLevel()-mtlp)*5;
        }
        
        /**
         * to string method
         * return name
         */
        public String toString() {
                return name;
        }
        
        /**
         * @param Planat to set the current planet
         */
        public void setPlanet(Planet p) {
        	this.planet = p;	
        }
        
        /**
         * @return Planet currentPlanet
         */
        public Planet getPlanet() {
        	return planet;
        }
}